package com.sie.controller;

import com.sie.dto.PageDto;
import com.sie.dto.RoleDto;
import com.sie.entity.Role;
import com.sie.service.AuthorityService;
import com.sie.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * 角色控制器
 */
@RestController
@SuppressWarnings("all")
@Api(tags = "角色管理控制器")
@RequestMapping("/role")
@CrossOrigin
public class RoleController {
    @Autowired
    private RoleService roleService;

    @Autowired
    private AuthorityService authorityService;

    @PostMapping("/findAll")
    @ApiOperation("查询所有角色")
    public Map findAll(HttpServletRequest request) {
        return roleService.findAll(request);
    }

    @PostMapping("/findAbyPage")
    @ApiOperation("分页查询")
    public Map findAll(@RequestBody PageDto<Role> dto, HttpServletRequest request) {
        return roleService.findByPage(dto.getData(), dto.getPageNum(), dto.getPageSize(), request);
    }

    @PostMapping("/add")
    @ApiOperation("新增角色")
    public Map addRole(@RequestBody Role role, HttpServletRequest request) {
        return roleService.addRole(request, role);
    }

    @PostMapping("/delete")
    @ApiOperation("根据id删除角色")
    public Map deleteRole(String id, HttpServletRequest request) {
        return roleService.deleteRoleById(id, request);
    }

    @PostMapping("/deleteList")
    @ApiOperation("根据id批量删除角色")
    public Map deleteRole(@RequestBody List<String> list, HttpServletRequest request) {
        return roleService.deleteRoleById(list, request);
    }

    @PostMapping("/update")
    @ApiOperation("修改角色")
    public Map update(@RequestBody Role role, HttpServletRequest request) {
        return roleService.updateRole(request, role);
    }

    @PostMapping("/findByName")
    @ApiOperation("角色名查重效验")
    public Map findByName(@RequestParam String name) {
        return roleService.findByName(name);
    }

    @ApiOperation("设置角色权限")
    @PostMapping("/setAuthority")
    public Map setAuthority(@RequestBody RoleDto dto, HttpServletRequest request) throws Exception {
        return authorityService.setAuthority(dto.getId(), dto.getList(), request);
    }

    @ApiOperation("获取合法角色编号")
    @PostMapping("/getNo")
    public Map getNo() {
        return roleService.getNo();
    }
}
